package com.pingsky.apm.agent.core.trace;

import com.pingsky.apm.agent.api.service.BootService;

public class ContextManager implements BootService {

    private static ThreadLocal<TracingContext> CONTEXT = new ThreadLocal<TracingContext>();

    private static TracingContext getOrCreate(String operationName) {
        TracingContext context = CONTEXT.get();
        if (context == null) {
            context = new TracingContext(operationName);
            CONTEXT.set(context);
        }
        return context;
    }

    private static TracingContext get() {
        TracingContext tracingContext = CONTEXT.get();
        if (null == tracingContext) {

        }
        return tracingContext;
    }

    public static TraceSpan createEntrySpan(String operationName, ContextCarrier carrier) {
        TracingContext context = getOrCreate(operationName);
        return context.createEntrySpan(operationName);
    }

    public static TraceSpan createLocalSpan(String operationName) {
        TracingContext tracingContext = get();
        return tracingContext.createEntrySpan(operationName);
    }

    @Override
    public void prepare() throws Throwable {

    }

    @Override
    public void start() throws Throwable {

    }
}
